package com.vieewer.btdemo.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.vieewer.btdemo.R;

public class CallToggleButton extends View {

    private Bitmap backgroundBitmap;// 背景图
    private Bitmap slidBtn;// 可以滑动的图片
    private int backgroundWidth, backgroundHeight, slidBtnWidth;
    private Paint paint;//画笔
    private float slidBtn_left;// 滑动按钮的左边距
    private int firstX;// 手机按下的x坐标
    private int lastX;// 手机抬起的x坐标
    public OnEventListener listener;

    public CallToggleButton(Context context) {
        super(context);
        initView();
    }

    public CallToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CallToggleButton(Context context, AttributeSet attrs,
                            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        //获取按钮背景
        backgroundBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.calling_bg);
        //options.inSampleSize = ScreenTools.calculateInSampleSize(options, 110, 160); // 110,160：转换后的宽和高，具体值会有些出入
        options.inJustDecodeBounds = false;
        //获取滑动按钮图
        slidBtn = BitmapFactory.decodeResource(getResources(),
                R.drawable.icon_phone_call_thumb);

        paint = new Paint();
        //设置抗锯齿
        paint.setAntiAlias(true);

        backgroundWidth = backgroundBitmap.getWidth();
        backgroundHeight = backgroundBitmap.getHeight();
        slidBtnWidth = slidBtn.getWidth();

        slidBtn_left = (backgroundWidth / 2) - (slidBtnWidth / 2);
    }

    /*
     * 测量view宽高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       // ScreenTools.init(getContext());
        // 设置宽高
        setMeasuredDimension(backgroundBitmap.getWidth(),
                backgroundBitmap.getHeight());
    }

    /*
     * 绘制当前view的内容
     */
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
        float h = (backgroundHeight / 2) - (slidBtn.getHeight() / 2);
        canvas.drawBitmap(slidBtn, slidBtn_left, h, paint);
    }

    /*
     * 滑动事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                firstX = lastX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                // 计算手指移动的距离
                int dis = (int) event.getX();
                // 根据手指的位置，改变slidBtn_left的值
                slidBtn_left = dis;
                break;

            case MotionEvent.ACTION_UP:
                int maxLeft = backgroundWidth - slidBtnWidth;
                if (slidBtn_left >= maxLeft) {
                    // 滑动到最右调用的方法
                    listener.RightEvent();
                } else if (slidBtn_left < (slidBtnWidth / 2)) {
                    // 滑动到最左调用的方法``
                    listener.LeftEvent();
                } else {
                    // 接听按钮回到原点
                    Log.i("simple", "接听按钮回到原点");
                    slidBtn_left = (backgroundWidth / 2) - (slidBtnWidth / 2);
                }
                break;
        }
        flushView();
        return true;
    }

    /**
     * 刷新当前视图
     */
    private void flushView() {
        int maxLeft = backgroundWidth - slidBtnWidth;
        // 确保 slideBtn_left >= 0
        slidBtn_left = (slidBtn_left > 0) ? slidBtn_left : 0;

        // 确保 slideBtn_left <=maxLeft
        slidBtn_left = (slidBtn_left < maxLeft) ? slidBtn_left : maxLeft;

        /*
         * 刷新当前视图调用invalidate 导致 执行onDraw执行
         */
        invalidate();
    }

    /**
     * view事件方法
     */
    public void setOnEvent(OnEventListener listener) {
        this.listener = listener;
    }




    public void moveToRight() {

        int maxLeft = backgroundWidth - slidBtnWidth - slidBtnWidth/4;
        slidBtn_left = maxLeft;
        flushView();

    }

    public void moveToLeft() {
        int maxLeft = backgroundWidth - slidBtnWidth;
        slidBtn_left = slidBtnWidth/4;
        flushView();
    }


    /**
     * view接口方法
     */
    public interface OnEventListener {
        abstract void RightEvent();

        abstract void LeftEvent();
    }

}